package com.huiyeji.dynamic.core.builder;

import com.huiyeji.dynamic.core.DynamicRegex;
import com.huiyeji.dynamic.core.RegexContainer;
import com.huiyeji.dynamic.core.provider.IntegerCalculationParamProvider;
import com.huiyeji.dynamic.core.provider.ParamProvider;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class IntegerCalculationParamProviderBuilder implements ParamProviderBuilder {

    public static final String integerCalculationRegex;

    static {
        integerCalculationRegex = "^(" + DynamicRegex.PARAM_CONTEXT_OBJECT_NAME.simple() + "|"
                + DynamicRegex.PARAM_INTEGER.simple() + ")\\s*"+getOperatorRegexes()+"\\s*("
                + DynamicRegex.PARAM_CONTEXT_OBJECT_NAME.simple() + "|"
                + DynamicRegex.PARAM_INTEGER.simple() + ")$";
    }

    @Override
    public ParamProvider build(String paramStr) {
        return new IntegerCalculationParamProvider(paramStr);
    }

    @Override
    public RegexContainer ruleInitialize() {
        return new RegexContainer(Collections.singletonList(
                IntegerCalculationParamProviderBuilder.integerCalculationRegex
        ));
    }

    private static String getOperatorRegexes() {
        StringBuilder operatorRegexes = new StringBuilder("(");
        for (String operator : getOperators()) {
            operatorRegexes.append(operator).append("|");
        }
        return operatorRegexes.append(")").toString();
    }

    private static List<String> getOperators(){
        return Arrays.asList(
                "\\+",
                "-",
                "/",
                "\\*",
                "==",
                ">=",
                "<=",
                ">",
                "<",
                "!="
        );
    }

    @Override
    public int getOrder() {
        return 4000;
    }
}
